import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/routers/jump_router.dart';
import 'package:tiktokadult/tools_base/global_store/local_ads_info_store.dart';

import '../../../hj_model/splash/ads_model.dart';
import '../../../tools_base/image/custom_network_image.dart';
import '../../../tools_base/widget/net_image_widget.dart';
import '../short_video_main_logic.dart';

class ShortFloatingAdView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ShortFloatingAdViewState();
  }
}

class _ShortFloatingAdViewState extends State<ShortFloatingAdView> {
  ShortVideoMainLogic get logic => Get.find<ShortVideoMainLogic>();
  static bool _isShowOpen = true;
  AdsInfoModel? _advertsInfo;

  double get _adSize => 80;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadAdData();
    });
  }

  void _loadAdData() async {
    var adsList = await getAdvByType(16);
    if (adsList.isNotEmpty == true) {
      _advertsInfo = adsList.first;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (_advertsInfo == null || !_isShowOpen) {
      return const SizedBox();
    }
    return Obx(() => Visibility(
          visible: logic.tabIndexState.value != 1 && logic.isMenuShowState.value,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: InkWell(
              onTap: () {
                if (!_isShowOpen) {
                  _isShowOpen = true;
                  setState(() {});
                } else {
                  if (_advertsInfo != null) {
                    pushToPageByLink(_advertsInfo?.href ?? "", id: _advertsInfo?.id);
                  }
                }
              },
              child: SizedBox(
                height: _adSize,
                width: _isShowOpen ? _adSize : 20,
                child: Stack(
                  children: [
                    Positioned(
                      top: 0,
                      left: 0,
                      child: NetworkImageLoader(
                        imageUrl: _advertsInfo?.cover ?? "",
                        height: _adSize,
                        width: _isShowOpen ? _adSize : 20,
                        borderRadius: 10,
                      ),
                    ),
                    if (_isShowOpen)
                      Positioned(
                        right: 0,
                        top: 0,
                        child: InkWell(
                          onTap: () {
                            _isShowOpen = false;
                            setState(() {});
                          },
                          child: Container(
                            padding: EdgeInsets.only(right: 3, top: 3),
                            child: Image.asset(
                              "close_cirle_grey.webp".commonImgPath,
                              width: 18,
                              height: 18,
                            ),
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ),
        ));
  }

  @override
  void dispose() {
    super.dispose();
  }
}
